package com.example.demo.mapper;

import com.example.demo.model.entity.achievement.VerticalProject;
import com.example.demo.model.enums.ProjectStatusEnum;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface VerticalProjectMapper {

    @Insert("INSERT INTO vertical_project (" +
            "project_name, approval_number, project_code, project_level, " +
            "total_funding, approval_department, project_type, start_date, end_date, " +
            "payment_records, participants, principal_investigator, financial_account, " +
            "completion_time, approval_document, completion_certificate, final_report, " +
            "create_time, update_time, status, submit_user_id" +
            ") VALUES (" +
            "#{projectName}, #{approvalNumber}, #{projectCode}, #{projectLevel}, " +
            "#{totalFunding}, #{approvalDepartment}, #{projectType}, #{startDate}, #{endDate}, " +
            "#{paymentRecords}, #{participants}, #{principalInvestigator}, #{financialAccount}, " +
            "#{completionTime}, #{approvalDocument}, #{completionCertificate}, #{finalReport}, " +
            "#{createTime}, #{updateTime}, #{status}, #{submitUserId}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(VerticalProject project);

    @Update("UPDATE vertical_project SET " +
            "project_name = #{projectName}, " +
            "approval_number = #{approvalNumber}, " +
            "project_code = #{projectCode}, " +
            "project_level = #{projectLevel}, " +
            "total_funding = #{totalFunding}, " +
            "approval_department = #{approvalDepartment}, " +
            "project_type = #{projectType}, " +
            "start_date = #{startDate}, " +
            "end_date = #{endDate}, " +
            "payment_records = #{paymentRecords}, " +
            "participants = #{participants}, " +
            "principal_investigator = #{principalInvestigator}, " +
            "financial_account = #{financialAccount}, " +
            "completion_time = #{completionTime}, " +
            "approval_document = #{approvalDocument}, " +
            "completion_certificate = #{completionCertificate}, " +
            "final_report = #{finalReport}, " +
            "update_time = #{updateTime}, " +
            "status = #{status} " +
            "WHERE id = #{id}")
    int update(VerticalProject project);

    @Delete("DELETE FROM vertical_project WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    @Select("SELECT * FROM vertical_project WHERE id = #{id}")
    @Results({
            @Result(property = "principalInvestigator", column = "principal_investigator"),
            @Result(property = "totalFunding", column = "total_funding"),
            @Result(property = "approvalDepartment", column = "approval_department"),
            @Result(property = "projectType", column = "project_type"),
            @Result(property = "startDate", column = "start_date"),
            @Result(property = "endDate", column = "end_date"),
            @Result(property = "approvalDocument", column = "approval_document"),
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    VerticalProject selectById(@Param("id") Long id);

    @Select("SELECT * FROM vertical_project")
    @Results({
            @Result(property = "principalInvestigator", column = "principal_investigator"),
            @Result(property = "totalFunding", column = "total_funding"),
            @Result(property = "approvalDepartment", column = "approval_department"),
            @Result(property = "projectType", column = "project_type"),
            @Result(property = "startDate", column = "start_date"),
            @Result(property = "endDate", column = "end_date"),
            @Result(property = "approvalDocument", column = "approval_document"),
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<VerticalProject> selectAll();

    @Select("SELECT * FROM vertical_project WHERE submit_user_id = #{userId}")
    @Results({
            @Result(property = "principalInvestigator", column = "principal_investigator"),
            @Result(property = "totalFunding", column = "total_funding"),
            @Result(property = "approvalDepartment", column = "approval_department"),
            @Result(property = "projectType", column = "project_type"),
            @Result(property = "startDate", column = "start_date"),
            @Result(property = "endDate", column = "end_date"),
            @Result(property = "approvalDocument", column = "approval_document"),
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<VerticalProject> selectBySubmitUserId(@Param("userId") Long userId);

    @Select("SELECT * FROM vertical_project WHERE status = #{status}")
    @Results({
            @Result(property = "principalInvestigator", column = "principal_investigator"),
            @Result(property = "totalFunding", column = "total_funding"),
            @Result(property = "approvalDepartment", column = "approval_department"),
            @Result(property = "projectType", column = "project_type"),
            @Result(property = "startDate", column = "start_date"),
            @Result(property = "endDate", column = "end_date"),
            @Result(property = "approvalDocument", column = "approval_document"),
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<VerticalProject> selectByStatus(@Param("status") ProjectStatusEnum status);

    @Select("SELECT * FROM vertical_project WHERE project_name LIKE CONCAT('%', #{name}, '%')")
    @Results({
            @Result(property = "principalInvestigator", column = "principal_investigator"),
            @Result(property = "totalFunding", column = "total_funding"),
            @Result(property = "approvalDepartment", column = "approval_department"),
            @Result(property = "projectType", column = "project_type"),
            @Result(property = "startDate", column = "start_date"),
            @Result(property = "endDate", column = "end_date"),
            @Result(property = "approvalDocument", column = "approval_document"),
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<VerticalProject> selectByProjectName(@Param("name") String name);

    @Select("SELECT * FROM vertical_project WHERE project_code = #{code}")
    @Results({
            @Result(property = "principalInvestigator", column = "principal_investigator"),
            @Result(property = "totalFunding", column = "total_funding"),
            @Result(property = "approvalDepartment", column = "approval_department"),
            @Result(property = "projectType", column = "project_type"),
            @Result(property = "startDate", column = "start_date"),
            @Result(property = "endDate", column = "end_date"),
            @Result(property = "approvalDocument", column = "approval_document"),
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    VerticalProject selectByProjectCode(@Param("code") String code);

    @Update("UPDATE vertical_project SET status = #{status}, update_time = #{updateTime} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") ProjectStatusEnum status, @Param("updateTime") LocalDateTime updateTime);

    @Select("SELECT COUNT(*) FROM vertical_project WHERE status = #{status}")
    int countByStatus(@Param("status") ProjectStatusEnum status);

    @Select("SELECT * FROM vertical_project WHERE project_level = #{level}")
    @Results({
            @Result(property = "principalInvestigator", column = "principal_investigator"),
            @Result(property = "totalFunding", column = "total_funding"),
            @Result(property = "approvalDepartment", column = "approval_department"),
            @Result(property = "projectType", column = "project_type"),
            @Result(property = "startDate", column = "start_date"),
            @Result(property = "endDate", column = "end_date"),
            @Result(property = "approvalDocument", column = "approval_document"),
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<VerticalProject> selectByProjectLevel(@Param("level") String level);
}